package org.geektimes.projects.user.web.controller;

import org.apache.commons.lang.StringUtils;
import org.eclipse.microprofile.config.Config;
import org.geektimes.web.mvc.controller.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import java.util.HashMap;
import java.util.Map;

/**
 * @author :   chuan
 * @date :   2021/03/24
 */
public class InitParameterController implements RestController {

    /**
     * Homework : http://localhost:8080/application/config?initParameterKey=servletInitParameterKey
     */
    @GET
    @Path("/application/config")
    public Object getInitParameter(String initParameterKey, HttpServletRequest request){

        final ServletContext servletContext = request.getServletContext();

        Config applicationConfig = (Config) servletContext.getAttribute("applicationConfig");

        final String value = applicationConfig.getValue(initParameterKey, String.class);

        Map<String, Object> result = new HashMap<>(3);
        result.put("sc", StringUtils.isBlank(value) ? 1000 : 1001);
        result.put("message", "GetInitParameter Success");
        result.put("value", value);

        return result;
    }
}
